package com.blogsoho.taurus.dao.builder;

import com.blogsoho.taurus.vo.ContentVO;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by 16978 on 2017/6/10.
 */
public class ContentSqlBuilder {

    private static final String CONTENT = "taurus_content";

    public String addContent(ContentVO content) {
        return new SQL() {{
            INSERT_INTO(CONTENT);
            VALUES("title", " #{title}");
            VALUES("text", " #{text}");
            VALUES("summary", " #{summary}");
            VALUES("link_to", " #{linkTo}");
            VALUES("markdown_enable", " #{markdownEnable}");
            VALUES("thumbnail", " #{thumbnail}");
            VALUES("module", " #{module}");
            VALUES("user_id", " #{userId}");
            VALUES("author", " #{author}");
            VALUES("user_email", " #{userEmail}");
            VALUES("user_ip", " #{userIp}");
            VALUES("user_agent", " #{userAgent}");
            VALUES("parent_id", " #{parentId}");
            VALUES("object_id", " #{objectId}");
            VALUES("order_number", " #{orderNumber}");
            VALUES("status", " #{status}");
            VALUES("vote_up", " #{voteUp}");
            VALUES("vote_down", " #{voteDown}");
            VALUES("rate", " #{rate}");
            VALUES("rate_count", " #{rateCount}");
            VALUES("comment_status", " #{commentStatus}");
            VALUES("comment_count", " #{commentCount}");
            VALUES("comment_time", " #{commentTime}");
            VALUES("view_count", " #{viewCount}");
            VALUES("modified", " #{modified}");
            VALUES("slug", " #{slug}");
            VALUES("flag", " #{flag}");
            VALUES("tag", " #{tag}");
            VALUES("remarks", " #{remarks}");
            VALUES("markdown_text", " #{markdownText}");
        }}.toString();
    }

    public String getAllContent(Map<String, Object> para) {
        String sql = new SQL() {{
            SELECT("*");
            FROM(CONTENT);
            WHERE("parent_id > 0");
        }}.toString();
        sql = sql + " limit " + para.get("start") + "," + para.get("length");
        return sql;
    }

    public String getCountContent() {
        return new SQL() {{
            SELECT("count(*)");
            FROM(CONTENT);
            WHERE("object_id is null");
            OR();
            WHERE("object_id <1");
        }}.toString();
    }

    public String getContentById(Map<String, Integer> map) {
        return new SQL() {{
            SELECT("*");
            FROM(CONTENT);
            WHERE("id=" + map.get("id").toString());
        }}.toString();
    }

    public String updateContent(ContentVO content){
        return new SQL(){{
            UPDATE(CONTENT);
            SET("title = #{title}");
            SET("text = #{text}");
            SET("summary = #{summary}");
            SET("link_to = #{linkTo}");
            SET("markdown_enable = #{markdownEnable}");
            SET("thumbnail = #{thumbnail}");
            SET("module = #{module}");
            SET("user_id = #{userId}");
            SET("author = #{author}");
            SET("user_email = #{userEmail}");
            SET("user_ip = #{userIp}");
            SET("user_agent = #{userAgent}");
            SET("parent_id = #{parentId}");
            SET("object_id = #{objectId}");
            SET("order_number = #{orderNumber}");
            SET("status = #{status}");
            SET("vote_up = #{voteUp}");
            SET("vote_down = #{voteDown}");
            SET("rate = #{rate}");
            SET("rate_count = #{rateCount}");
            SET("comment_status = #{commentStatus}");
            SET("comment_count = #{commentCount}");
            SET("comment_time = #{commentTime}");
            SET("view_count = #{viewCount}");
            SET("modified = #{modified}");
            SET("slug = #{slug}");
            SET("flag = #{flag}");
            SET("tag = #{tag}");
            SET("remarks = #{remarks}");
            SET("markdown_text = #{markdownText}");
            WHERE("id=#{id}");
        }}.toString();
    }

    public String getContentByCategoryId(Map<String, Integer> map) {
        return new SQL() {{
            SELECT("*");
            FROM(CONTENT);
            WHERE("parent_id=" + map.get("id").toString());
        }}.toString();
    }
}
